/**
 * Created by jihb on 4/13/16.
 */
Ext.define('JYnovel.controller.system.IndexController',{
    extend:'Ext.app.Controller',
    views:[
        'system.IndexView'
    ],
    models:[
        'system.MenuModel'
    ],
    stores:[
        'system.MenuStore',
        'system.MenuTreeStore'
    ],
    refs:[{
        ref:'IndexLeftPanel',
        selector:'IndexLeftPanel'
    },{
        ref:'IndexContentTabPanel',
        selector:'IndexContentTabPanel'
    },{
        ref:'IndexView',
        selector:'IndexView'
    }],
    init:function () {
        this.control({
            'IndexTopPanel label[name=welcomeLabel]':{
                boxready:this.welcomeLabelBoxready
            },
            'IndexTopPanel button[name=changePwdBtn]':{
                click:this.changePwdBtnClick
            },
            'IndexTopPanel button[name=loginOutBtn]':{
                click:this.loginOutBtnClick
            },
            'IndexLeftPanel':{
                afterrender:this.indexLeftPanelAfterrender
            },
            'IndexMenuTree':{
                itemclick:this.menuTreeItemClick
            },
            'IndexContentHtmlPanel':{
                afterrender:this.indexContentHtmlPanelAfterrender
            }
        });
    },
    /**
     * 显示用户名称
     * @param label
     * @param width
     * @param height
     * @param eOpts
     */
    welcomeLabelBoxready:function (label, width, height, eOpts) {
        label.setText("用户:"+loginUserName);
    },
    /**
     * 修改密码
     */
    changePwdBtnClick:function () {

    },
    /**
     * 退出登录
     */
    loginOutBtnClick:function () {
        var indexView = this.getIndexView();
        Ext.Msg.confirm('','确定退出吗?',function (btn) {
           if(btn=='yes'){
               ExtAjaxSubmit({
                   url:getUrl('/loginout'),
                   method:'POST',
                   masktarget:indexView
               },function (data) {
                   if(data.code==0){
                       parent.window.location.href = getUrl('/login');
                   }
               });
           }
        });
    },
    /**
     * 左侧菜单panel加载完成
     * @param panel
     * @param eOpts
     */
    indexLeftPanelAfterrender:function (panel,eOpts) {
        var me = this;
        var loader = panel.getLoader();
        loader.on('load',function (loader, response, options, eOpts ) {
            var responseText = response.responseText;
            var dataJson = Ext.decode(responseText);
            if (dataJson){
                me.addMenuToShow(dataJson.result);
            }
        });
        loader.load();
    },

    addMenuToShow:function (rootMenuList) {
        if(!rootMenuList){
            return;
        }
        var indexLeftPanel = this.getIndexLeftPanel();
        for (var i=0;i<rootMenuList.length;i++){
            var menu = rootMenuList[i];
            var treeStore = Ext.create('JYnovel.store.system.MenuTreeStore',{
                defaultRootId:menu.id
            });
            var menuItem = Ext.create('JYnovel.view.system.IndexMenuTree',{
                title:menu.name,
                store:treeStore
            });
            indexLeftPanel.add(menuItem);
        }
    },
    menuTreeItemClick:function (tree, record, item, index, e, eOpts) {
        var tabPanel = this.getIndexContentTabPanel();
        var contentItem = Ext.getCmp('tab_'+record.get('id'));
        if (!contentItem){
            contentItem = Ext.widget('IndexContentPanel',{
                id:'tab_'+record.get('id'),
                title:record.get('name'),
                html:'<iframe width="100%" scrolling="no" frameborder="0" height="100%" src="'+getUrl(record.get('path'))+'"></iframe>'
            });
            tabPanel.add(contentItem);
        }
        tabPanel.setActiveTab(contentItem);
    },
    indexContentHtmlPanelAfterrender:function (panel,eOpts) {
        // var loader = panel.getLoader();
        // loader.url = getUrl(panel.menuPath);
        // loader.load();
    }
});